package biz.datalk.industrialland.admin.config;

import biz.datalk.industrialland.admin.interceptor.CustomCheckSignatureInterceptor;
import biz.datalk.industrialland.common.component.interceptor.DatalkAuthInterceptor;
import biz.datalk.industrialland.common.def.CommonConstant;
import biz.datalk.industrialland.common.json.JsonReturnHandler;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.validation.Validator;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;

import java.util.Arrays;
import java.util.List;

/**
 * spring mvc 配置
 *
 * @author       tarofang@163.com
 * @date         2019年08月10日
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

	@Autowired
	private Validator customValidator;

	@Value("${datalk.staticExcludePathPatterns:}")
	private String staticExcludePathPatterns;

	private final CustomCheckSignatureInterceptor checkSignatureInterceptor;

	@Autowired
	public WebMvcConfig(CustomCheckSignatureInterceptor checkSignatureInterceptor) {
		this.checkSignatureInterceptor = checkSignatureInterceptor;
	}

	/**
	 * 静态资源的配置 - 使得可以从磁盘中读取 Html、图片、视频、音频等
	 */
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// ****************************************** 可造成 任意文件下载 漏洞 ********************
		// ***************** 部署在 linux 上
		// ***************** http://192.168.56.104::9900/sys/index../../../../../../etc/passwd
		//registry.addResourceHandler("/**")
		//.addResourceLocations("file:" + upLoadPath + "//", "file:" + webAppPath + "//")
		//.addResourceLocations(staticLocations.split(","));
		// *********************************************************************************
	}

	/**
	 * 访问根路径默认跳转 index.ftl 页面 （简化部署方案： 可以把前端打包直接放到项目的 webapp，上面的配置）
	 */
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		// registry.addViewController("/").setViewName("index");
		// // 注册错误页面
		// registry.addViewController("/403").setViewName("biz/datalk/framework/core/error/403");
		// registry.addViewController("/404").setViewName("biz/datalk/framework/core/error/404");
		// registry.addViewController("/500").setViewName("biz/datalk/framework/core/error/500");
	}

	/** 添加拦截器 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		String staticExcludePathPatterns = StringUtils.trimToEmpty(this.staticExcludePathPatterns);
		String[] excludePatterns = StringUtils.split(staticExcludePathPatterns, ",");
		if ((excludePatterns != null) && (excludePatterns.length > 0)) {
			excludePatterns = Arrays.stream(excludePatterns).map(String::trim).toArray(String[]::new);
			
		}
		
		// 国际化变更
		LocaleChangeInterceptor localeChangeInterceptor = new LocaleChangeInterceptor();
		localeChangeInterceptor.setParamName(CommonConstant.I18N_PARAM);
		registry.addInterceptor(localeChangeInterceptor).order(0).addPathPatterns("/**");

		// 授权验证 intercept
		// registry.addInterceptor(new MessageResourceInterceptor()).addPathPatterns("/**");
		registry.addInterceptor(new DatalkAuthInterceptor()).order(1).addPathPatterns("/**").excludePathPatterns(excludePatterns);

		// 校核签名的拦截器
		if (checkSignatureInterceptor != null) {
			registry.addInterceptor(checkSignatureInterceptor).order(2).addPathPatterns("/**").excludePathPatterns(excludePatterns);
		}
	}

	/** 自定义验证器 */
	@Override
	public Validator getValidator() {
		return customValidator;
	}

	/** 添加自定义 消息转换器 */
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
	}

	// 转移到 加密 中
	/** 注册Bean  Json 返回值处理器 */
	@Bean
	public JsonReturnHandler jsonReturnHandler() {
		return new JsonReturnHandler();
	}

	/** 添加自定义返回值处理器 */
	@Override
	public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> handlers) {
		handlers.add(jsonReturnHandler());
	}

}
